package com.lightsaber.trade.as.web.retail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.entity.reta.OrderTemplate;
import com.lightsaber.trade.as.entity.reta.OrderTemplateItem;
import com.lightsaber.trade.as.entity.reta.Product;
import com.lightsaber.trade.as.service.reta.OrderTemplateItemManager;
import com.lightsaber.trade.as.service.reta.OrderTemplateManager;
import com.lightsaber.trade.as.service.reta.ProductManager;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/retail")
public class OrderTemplateItemAction extends BaseAction implements SessionAware {

    private Map<String, Object> session;

    @Autowired
    private OrderTemplateItemManager orderTemplateItemManager;

    @Autowired
    private OrderTemplateManager orderTemplateManager;

    @Autowired
    private ProductManager productManager;

    // -------------------------------------product search START
    @Value(value = "#{appProperties['retail.page.size']}")
    private String PAGE_SIZE;
    private Page<Product> page;
    private Product product;
    private String[] ids;
    public static final String SELECTED_PRODUCT_MAP = "SELECTED_PRODUCT_FOR_ORDER_MAP";
    // -------------------------------------product search END

    private String orderTemplateId;
    private boolean clear;
    private OrderTemplateItem orderTemplateItem;
    private List<OrderTemplateItem> results;

    @Override
    public String execute() throws Exception {
        if (clear) {
            orderTemplateItem = null;
        }

        OrderTemplate orderTemplate = orderTemplateManager.get(orderTemplateId);
        results = orderTemplate.getOrderTemplateItemList();
        
        return SUCCESS;
    }

    public String delete() throws Exception {
        orderTemplateItemManager.delete(orderTemplateItem.getId());
        orderTemplateManager.onItemChanged(orderTemplateId);
        
        return execute();
    }

    public String batchDelete() throws Exception {
        orderTemplateItemManager.delete(ids);
        orderTemplateManager.onItemChanged(orderTemplateId);
        
        return execute();
    }

    public String saveNumber() throws Exception {
        orderTemplateItemManager.saveNumber(results);
        orderTemplateManager.onItemChanged(orderTemplateId);
        
        return execute();
    }

    public String add() throws Exception {
        // 第一次进入这个页面要清楚session
        session.remove(SELECTED_PRODUCT_MAP);

        return searchProducts();
    }

    public String searchProducts() throws Exception {
        getSelectedCodesMap();

        if (clear) {
            product = null;
        }

        if (page == null) {// Click 'Search' button first
            page = new Page<Product>(Integer.valueOf(PAGE_SIZE));
        } else {// Click 'Search' button
            page.setPageSize(Integer.valueOf(PAGE_SIZE));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("name");
            page.setOrder(Page.ASC);
        }

        productManager.findPage(product, page);

        return INPUT;
    }

    public String confirmSelectedProducts() throws Exception {
        Map<String, String> selectedIdsMap = getSelectedCodesMap();

        orderTemplateItemManager.confirmSelectedProducts(orderTemplateId, selectedIdsMap);
        session.remove(SELECTED_PRODUCT_MAP);
        orderTemplateManager.onItemChanged(orderTemplateId);
        
        return execute();
    }

    public OrderTemplateItem getOrderTemplateItem() {
        return orderTemplateItem;
    }

    public void setOrderTemplateItem(OrderTemplateItem orderTemplateItem) {
        this.orderTemplateItem = orderTemplateItem;
    }

    public boolean isClear() {
        return clear;
    }

    public void setClear(boolean clear) {
        this.clear = clear;
    }

    public String getOrderTemplateId() {
        return orderTemplateId;
    }

    public void setOrderTemplateId(String orderTemplateId) {
        this.orderTemplateId = orderTemplateId;
    }

    public List<OrderTemplateItem> getResults() {
        return results;
    }

    public void setResults(List<OrderTemplateItem> results) {
        this.results = results;
    }

    public Page<Product> getPage() {
        return page;
    }

    public void setPage(Page<Product> page) {
        this.page = page;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    public String[] getIds() {
        return ids;
    }

    public void setIds(String[] ids) {
        this.ids = ids;
    }

    private Map<String, String> getSelectedCodesMap() {
        Map<String, String> selectedIdsMap = (Map<String, String>) session
                .get(SELECTED_PRODUCT_MAP);

        if (selectedIdsMap == null) {
            selectedIdsMap = new HashMap<String, String>();
            session.put(SELECTED_PRODUCT_MAP, selectedIdsMap);
        }

        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                selectedIdsMap.put(id, id);
            }
        }

        return selectedIdsMap;
    }

}
